{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d224ba8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import serial\n",
    "import struct\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a60c6a4c",
   "metadata": {},
   "source": [
    "## 正反转各1秒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ec480e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "ser = serial.Serial('COM3', 921600, timeout=1)\n",
    "try:\n",
    "    # Enter \"AT command mode\"\n",
    "    ser.write(bytes.fromhex('41 54 2b 41 54 0d 0a'))\n",
    "    \n",
    "    # Rotate the motor forward for 1 second\n",
    "    ser.write(bytes.fromhex('41 54 90 07 eb fc 08 05 70 00 00 07 01 95 54 0d 0a'))\n",
    "    time.sleep(1)\n",
    "    \n",
    "    # Stop the motor for 1 second\n",
    "    ser.write(bytes.fromhex('41 54 90 07 eb fc 08 05 70 00 00 07 00 7f ff 0d 0a'))\n",
    "    time.sleep(1)\n",
    "    \n",
    "    # Rotate the motor backward for 1 second\n",
    "    ser.write(bytes.fromhex('41 54 90 07 eb fc 08 05 70 00 00 07 01 6a aa 0d 0a'))\n",
    "    time.sleep(1)\n",
    "    \n",
    "    # Stop the motor for 1 second\n",
    "    ser.write(bytes.fromhex('41 54 90 07 eb fc 08 05 70 00 00 07 00 7f ff 0d 0a'))\n",
    "    time.sleep(1)\n",
    "\n",
    "except Exception as e:\n",
    "    print(f\"An error occurred: {e}\")\n",
    "\n",
    "finally:\n",
    "    # Close the serial port\n",
    "    ser.close()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6400aea3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
